//
//  MKWaterFallLayout.swift
//  MOKOAnnounce
//
//  Created by 曹相召 on 2017/7/11.
//  Copyright © 2017年 cc.moko. All rights reserved.
//  瀑布流布局

import UIKit
@objc protocol MKWaterFallLayoutProtocol {
   @objc optional func waterLayout(waterLayout:MKWaterFallLayout,indexPath:IndexPath,itemWidth:CGFloat) -> CGFloat
}
class MKWaterFallLayout: UICollectionViewFlowLayout {
    weak var delegate:MKWaterFallLayoutProtocol? //代理
    var columnMargin:CGFloat!               //列间距
    var rowMargin:CGFloat!                  //行间距
    var edge:UIEdgeInsets!                  //边缘间距
    var columnCount:Int!                    //列数

    var attributesArray:[UICollectionViewLayoutAttributes] = [UICollectionViewLayoutAttributes]()  //存放所有cell的布局属性
    var allColumnMaxYArray:[CGFloat] = [CGFloat]()  //所有列的高度
    var contentSizeHeight:CGFloat?   //内容的高度

    override func prepare() {
        super.prepare()
        self.contentSizeHeight = 0
        self.attributesArray.removeAll()
        self.allColumnMaxYArray.removeAll()
        //三列都从edge.top开始布局
        for _ in 0..<self.columnCount! {
            self.allColumnMaxYArray.append((self.edge?.top)!)
        }
        //获取item的个数
        let count = self.collectionView?.numberOfItems(inSection: 0)
        for index in 0..<count!{
            let indexPath:IndexPath = IndexPath.init(item: index, section: 0)
            let attributes:UICollectionViewLayoutAttributes = self.layoutAttributesForItem(at: indexPath as IndexPath)!
            self.attributesArray.append(attributes)
        }
    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        return self.attributesArray
    }
    
    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?{
        let attributes:UICollectionViewLayoutAttributes = UICollectionViewLayoutAttributes.init(forCellWith: indexPath)
        let collectionViewW = (self.collectionView?.frame.size.width)!
        var column = 0
        var minColumnHeight = self.allColumnMaxYArray[0]
        
        for index in 0..<self.columnCount! {
            
            let columnHeight = self.allColumnMaxYArray[index]
            if(minColumnHeight > columnHeight){
                minColumnHeight = columnHeight
                column = index
            }
        }
    
        let w = (collectionViewW - (self.edge?.left)! - (self.edge?.right)! - CGFloat(self.columnCount! - 1) * self.columnMargin!) / CGFloat(self.columnCount!)
    
        let x = self.edge!.left + (self.columnMargin! + w) * CGFloat(column)

        var y = minColumnHeight
        
        if(y != self.edge?.top){
            y += self.rowMargin!
        }
        
        let h = (self.delegate?.waterLayout!(waterLayout: self, indexPath:indexPath, itemWidth: w))!
        attributes.frame = CGRect.init(x: x, y: y, width: w, height: h)
        self.allColumnMaxYArray[column] = attributes.frame.maxY
        
        let columHeight = self.allColumnMaxYArray[column]
        if(self.contentSizeHeight! < columHeight){
            self.contentSizeHeight = columHeight
        }
        return attributes
    }
    
    override var collectionViewContentSize: CGSize {
        return CGSize.init(width: 0.1, height: self.contentSizeHeight!+self.edge!.bottom)
    }
    
}
